/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Services;

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import Entity.Person;
import Entity.Veterinarian;
import Services.exceptions.IllegalOrphanException;
import Services.exceptions.NonexistentEntityException;
import Services.exceptions.PreexistingEntityException;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author itachiu
 */
public class VeterinarianJpaController implements Serializable {

    public VeterinarianJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Veterinarian veterinarian) throws IllegalOrphanException, PreexistingEntityException, Exception {
        List<String> illegalOrphanMessages = null;
        Person personOrphanCheck = veterinarian.getPerson();
        if (personOrphanCheck != null) {
            Veterinarian oldVeterinarianOfPerson = personOrphanCheck.getVeterinarian();
            if (oldVeterinarianOfPerson != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("The Person " + personOrphanCheck + " already has an item of type Veterinarian whose person column cannot be null. Please make another selection for the person field.");
            }
        }
        if (illegalOrphanMessages != null) {
            throw new IllegalOrphanException(illegalOrphanMessages);
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Person person = veterinarian.getPerson();
            if (person != null) {
                person = em.getReference(person.getClass(), person.getPersonId());
                veterinarian.setPerson(person);
            }
            em.persist(veterinarian);
            if (person != null) {
                person.setVeterinarian(veterinarian);
                person = em.merge(person);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findVeterinarian(veterinarian.getPersonPersonId()) != null) {
                throw new PreexistingEntityException("Veterinarian " + veterinarian + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Veterinarian veterinarian) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Veterinarian persistentVeterinarian = em.find(Veterinarian.class, veterinarian.getPersonPersonId());
            Person personOld = persistentVeterinarian.getPerson();
            Person personNew = veterinarian.getPerson();
            List<String> illegalOrphanMessages = null;
            if (personNew != null && !personNew.equals(personOld)) {
                Veterinarian oldVeterinarianOfPerson = personNew.getVeterinarian();
                if (oldVeterinarianOfPerson != null) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("The Person " + personNew + " already has an item of type Veterinarian whose person column cannot be null. Please make another selection for the person field.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (personNew != null) {
                personNew = em.getReference(personNew.getClass(), personNew.getPersonId());
                veterinarian.setPerson(personNew);
            }
            veterinarian = em.merge(veterinarian);
            if (personOld != null && !personOld.equals(personNew)) {
                personOld.setVeterinarian(null);
                personOld = em.merge(personOld);
            }
            if (personNew != null && !personNew.equals(personOld)) {
                personNew.setVeterinarian(veterinarian);
                personNew = em.merge(personNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = veterinarian.getPersonPersonId();
                if (findVeterinarian(id) == null) {
                    throw new NonexistentEntityException("The veterinarian with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Veterinarian veterinarian;
            try {
                veterinarian = em.getReference(Veterinarian.class, id);
                veterinarian.getPersonPersonId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The veterinarian with id " + id + " no longer exists.", enfe);
            }
            Person person = veterinarian.getPerson();
            if (person != null) {
                person.setVeterinarian(null);
                person = em.merge(person);
            }
            em.remove(veterinarian);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Veterinarian> findVeterinarianEntities() {
        return findVeterinarianEntities(true, -1, -1);
    }

    public List<Veterinarian> findVeterinarianEntities(int maxResults, int firstResult) {
        return findVeterinarianEntities(false, maxResults, firstResult);
    }

    private List<Veterinarian> findVeterinarianEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Veterinarian.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Veterinarian findVeterinarian(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Veterinarian.class, id);
        } finally {
            em.close();
        }
    }

    public int getVeterinarianCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Veterinarian> rt = cq.from(Veterinarian.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
